﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk.Client;
using DataFlock.CRM.Engine.Common;
using Microsoft.Xrm.Sdk.Discovery;

namespace DataFlock.CRM.Engine
{
    public class Organization
    {
        private static string _organizationService;
        private static string _organizationDataService;
        private static string _webApplication;
        private static OrganizationDetail _organisationInScope;

        public static Uri HomeRealmUri
        {
            get
            {
                return null;
            }
        }
        public static Uri OrganizationService
        {
            get
            {
                return new Uri(_organizationService);
            }
        }

        public static Uri OrganizationDataService
        {
            get
            {
                return new Uri(_organizationDataService);
            }
        }

        public static Uri WebApplication
        {
            get
            {
                return new Uri(_webApplication);
            }
        }

        public static OrganizationDetail OrganizationInScope
        {
            get
            {
                return _organisationInScope;
            }
        }

        public static OrganizationDetailCollection GetOrganizations()
        {
            using (DiscoveryServiceProxy serviceProxy = new DiscoveryServiceProxy(Configuration.DiscoveryServiceUrl, null, Security.Credentials, Security.DeviceCredentials))
            {
                RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
                RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)serviceProxy.Execute(orgRequest);
                return orgResponse.Details;
            }
        }

        public static void SetDefaultOrganization()
        {
            OrganizationDetail orgDetail = GetOrganizations().FirstOrDefault();
            _organizationService = orgDetail.Endpoints[EndpointType.OrganizationService];
            _organizationDataService = orgDetail.Endpoints[EndpointType.OrganizationDataService];
            _webApplication = orgDetail.Endpoints[EndpointType.WebApplication];
            _organisationInScope = orgDetail;
        }
        
        public static void SetOrganization(string orgUniqueName)
        {
            OrganizationDetail orgDetail = FindOrganizationByUniqueName(orgUniqueName);
            _organizationService = orgDetail.Endpoints[EndpointType.OrganizationService];
            _organizationDataService = orgDetail.Endpoints[EndpointType.OrganizationDataService];
            _webApplication = orgDetail.Endpoints[EndpointType.WebApplication];
            _organisationInScope = orgDetail;
        }

        public static OrganizationDetail FindOrganizationByFriendlyName(string orgFriendlyName)
        {
            return FindOrganizationByFriendlyName(orgFriendlyName, GetOrganizations());
        }

        public static OrganizationDetail FindOrganizationByFriendlyName(string orgFriendlyName, OrganizationDetailCollection orgDetails)
        {
            return orgDetails.Where(o => o.FriendlyName == orgFriendlyName).FirstOrDefault();
        }

        public static OrganizationDetail FindOrganizationByUniqueName(string orgUniqueName)
        {
            return FindOrganizationByUniqueName(orgUniqueName, GetOrganizations());
        }

        public static OrganizationDetail FindOrganizationByUniqueName(string orgUniqueName, OrganizationDetailCollection orgDetails)
        {
            return orgDetails.Where(o => o.UniqueName == orgUniqueName).FirstOrDefault();
        }
    }
}
